<?php

namespace App\Http\Controllers;

use App\Models\MyMessage;

use App\Http\Libs\Helper_Huanxin;
use App\Models\Throughtrain;
use App\Models\User;
use Illuminate\Http\Request;
use \DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\ThroughtrainHoldApply;
use App\Models\RoleUser;

class ZtcApplyController extends CommonController
{
    /**
     * 获取直通车申请列表
     *
     * @return Response
     */
    public function getList()
    {
        $lists = ThroughtrainHoldApply::with('user')->orderBy('created_at', 'desc')->paginate(10);

        return view('ztcapply.list')->with('lists', $lists)->with("actions", $this->actions);
    }


    /**
     * 执行一个搜索
     */
    public function search(Request $request)
    {
        $data = $request->input();
        $sql = ThroughtrainHoldApply::with('user');

        if (isset($data['username']) && $data['username'] != '') {
            $uids = User::where('username', 'like', '%' . trim($data['username']) . '%')->lists('uid');

            $sql->whereIn('uid', $uids);
        }

        if (isset($data['nickname']) && $data['nickname'] != '') {

            $uids = User::where('realname', 'like', '%' . trim($data['nickname']) . '%')->orWhere('nickname', 'like', '%' . trim($data['nickname']) . '%')->lists('uid');
            $sql->whereIn('uid', $uids);
        }


        if (isset($data['status']) && $data['status'] != '') {
            $sql->where('status', $data['status']);
        }

        $lists = $sql->paginate(10);

        return view('ztcapply.search')->with("lists", $lists)->with("username", $data['username'])
            ->with("nickname", $data['nickname'])->with("status", $data['status'])
            ->with("actions", $this->actions);

    }


    /**
     * 获取某个直通车审核页面
     *
     */
    public function getAudit(Request $request)
    {
        $id = $request->input('id');
        $info = ThroughtrainHoldApply::with('user')->where('id', '=', $id)->first();

        return view('ztcapply.audit')->with('info', $info);
    }

    /**
     * 审核一个权限申请
     *
     * @param Request $request
     */
    public function postAudit(Request $request)
    {
        $this->validate($request, ['status' => 'required|in:1,-1']);
        $data = $request->input();

        $ztcholdapply = DB::table('throughtrain_hold_apply')
            ->where('id', '=', $data['id'])->first();

        ThroughtrainHoldApply::where('id', '=', $data['id'])->update(['status' => $data['status']]);
        $user = User::where('uid', $ztcholdapply->uid)->first();
        if ($data['status'] == -1) {
            User::where('uid', $data['uid'])->update(['is_ztc' => 0]);
            $m_title='很抱歉您申请的资方直通车没有成功';
            $m_content='<p>你于'.date('Y年m月d日',strtotime($ztcholdapply->created_at)).'申请的“资方直通车”没有成功。</p>
                        <p>失败原因：您还没有无界投融直通车硬件设备或存在疑问。</p>';
            MyMessage::createMessage($user->uid,$m_title,'official',$m_content,'','');
            return AjaxCallbackMessage('审核未通过,该投资人未获得直通车资格', true, url('ztcapply/list'));
        }

        User::where('uid', $data['uid'])->update(['is_ztc' => 1]);
        Throughtrain::Create(['uid' => $data['uid'], 'status' => 1, 'time_type' => 1]);

        if (!$ztcholdapply->status && $data['status']) {
        	/***通知客户端刷新***/
        	@Helper_Huanxin::sendMessage(array($user->uid), "已成功开通直通车","admin",array("type"=>"notice_reload"));
            $content_sms = trans('sms.ztcApplySuccess');
            @SendSMS($user->username, $content_sms, 'ztcApplySuccess');
            $content_app = trans('app.ztcApplySuccess');

            $m_title='恭喜您，您申请的资方直通车已经通过审核';
            $m_content='恭喜您于'.date('Y年m月d日',time()).'申请的“资方直通车”已经审核通过，你已获得以下权限：';
            $m_ext='<div class="mt10 mb10 dark2_gray">
	   	            <p>你已获得以下权限：</p>
	   	            <div class="lightgray2">
	   	  	        <p>① / 无界投融直通车硬件设备，可以实现随时随刻邀请项目直通</p>
	   	            <p>② / 开启项目申请直通入口，项目方可以申请与你时时直通，在办公室就可以实现路演操作，效率翻倍</p>
	   	            <p>③ / 加入无界投融天涯云计划，实现全球共联</p>
	   	            </div>
	                </div>
	                <div class="borderLine"></div>';
            MyMessage::createMessage($user->uid,$m_title,'official',$m_content,$m_ext,'');

        }

        return AjaxCallbackMessage('审核成功', true, url('ztcapply/list'));
    }
}
